package cn.antalk.top.auth.service;

import cn.antalk.top.auth.client.UserClient;
import cn.antalk.top.auth.config.JwtProperties;
import cn.antalk.top.auth.utils.JwtUtils;
import cn.antalk.top.common.to.UserInfo;
import cn.antalk.top.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @Author: WackyBoy
 * @Date: 2021/12/6 18:23
 */
@Service
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties properties;

    public String authentication(String username, String password) {
        try {
            ResponseEntity<User> res = this.userClient.queryUser(username, password);
            User user = res.getBody();
            if(user == null) return null;
            // 添加信息到token
            String token = JwtUtils.generateToken(new UserInfo(user.getUid(), user.getUname(), user.getAvatar()), properties.getPrivateKey(), properties.getExpire());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
